package com.bigcms.controller;


import com.bigcms.domain.Type;
import com.bigcms.service.ArtTypeService;
import com.bigcms.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/v1/pri/type")
public class ArtTypeController {
    @Autowired
    ArtTypeService artTypeService;
    /**
     * 分类保存
     * @param type
     * @param request
     * @return
     */
    @RequestMapping("save")
    public JsonData save(@RequestBody Type type, HttpServletRequest request){
        Integer id = (Integer) request.getAttribute("uid");
        type.setUid(id);
        type.setCreateTime( (int) (System.currentTimeMillis() / 1000));
        artTypeService.addType(type);
        return JsonData.Success();
    }
    /**
     * 分类修改
     * @param type
     * @param request
     * @return
     */
    @RequestMapping("edit")
    public JsonData edit(@RequestBody Type type, HttpServletRequest request){
        Integer id = (Integer) request.getAttribute("uid");
        Type is_have = artTypeService.getType(type.getId());
        if(is_have==null){
            return JsonData.Error("分类不存在");
        }
        if(is_have.getUid()!=id){
            return JsonData.Error("此分类不是你的");
        }
        type.setUid(id);
        artTypeService.editType(type);
        return JsonData.Success();
    }
    /**
     * 获取全部分类
     * @return
     */
    @RequestMapping("list")
    public JsonData list(){
        List<Type> list = artTypeService.getTypes();
        return JsonData.Success(list);
    }
    /**
     * 获取单个分类
     * @return
     */
    @RequestMapping("get")
    public JsonData get(@RequestBody Type type){
        Type list = artTypeService.getType(type.getId());
        return JsonData.Success(list);
    }
    /**
     * 删除单个分类
     * @return
     */
    @RequestMapping("del")
    public JsonData del(@RequestBody Type type){
        artTypeService.delType(type.getId());
        return JsonData.Success("删除成功");
    }
}
